package com.zl.rbac.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zl.rbac.entity.Permission;
import com.zl.rbac.mapper.PermissionMapper;
import com.zl.rbac.mapper.RolePermissionMapper;
import com.zl.rbac.service.IPermissionService;
import com.zl.rbac.util.PermissionAnnotationUtils;
import com.zl.rbac.util.RequiredPermission;
import com.zl.rbac.util.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 权限的业务类
 * @ClassName: PermissionServiceImpl
 * @author: suke
 * @since: 2021/1/11 14:06
 */
@Service
public class PermissionServiceImpl implements IPermissionService{
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private PermissionAnnotationUtils permissionAnnotationUtils;
    @Autowired
    private RolePermissionMapper  rolePermissionMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void load() throws Exception {
        //1.扫描整个项目权限
       Map<String,List<Permission>> permissionMap =   permissionAnnotationUtils.getMethodInfoByAnnoation(SystemConstant.SCANNER_BASE_PACKAGE,Controller.class, RequiredPermission.class);
       //2.先查询数据库表中的数据
       List<Permission> dbPermissions =  permissionMapper.selectList(null);
       //对Map进行遍历
        permissionMap.forEach((key,permissions)->{
            //对List<Permission> 遍历
            permissions.forEach((permission)->{
                //判断权限是否存在
                //如果不存在, 保存到数据  集合对象.contains(对象)   前提条件, 整个Permission类重写hashCode() 与equals()
                if(!dbPermissions.contains(permission)){
                    permissionMapper.insert(permission); //3.保存到数据库, 用户重复点击, 出现重复加载
                }
            });
        });

    }

    @Override
    public List<Permission> queryByRoleId(Integer roleId) throws Exception {
        return permissionMapper.queryByRoleId(roleId);
    }

    @Override
    public Page<Permission> queryByPage(Integer pageIndex, Integer pageSize) throws Exception {
        Page<Permission> page = new Page<>(pageIndex,pageSize);
        //设置排序, 根基id升序排序
        page.addOrder(new OrderItem("id",true));
        permissionMapper.selectPage(page,null);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Serializable id) throws Exception {
        //删除权限表
        permissionMapper.deleteById(id);
        //删除中间表  tb_role_permission
        rolePermissionMapper.deleteByPid((Integer)id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Permission permission) throws Exception {
        permissionMapper.insert(permission);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Permission permission) throws Exception {
        permissionMapper.updateById(permission);
    }

    @Override
    public Permission queryById(Serializable id) throws Exception {
        return permissionMapper.selectById(id);
    }

    @Override
    public List<Permission> queryAll() throws Exception {
        return permissionMapper.selectList(null);
    }




}
